package com.yq.cp.controller.yw;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.yw.YwBjgl;
import com.yq.cp.entity.yw.YwYwfySq;
import com.yq.cp.entity.yw.YwYwfySqVo;
import com.yq.cp.entity.yw.dto.YwYwfySqDto;
import com.yq.cp.entity.yw.vo.YwGdxxVo;
import com.yq.cp.service.yw.YwYwfySqService;
import com.yq.cp.utils.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 运维费用-申请 前端控制器
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/yw-ywfy-sq")
public class YwYwfySqController {

    @Resource
    YwYwfySqService ywYwfySqService;
    @ResponseBody
    @PostMapping("add")
    public ApiResult add(@RequestBody YwYwfySq ywYwfySq) {
        ywYwfySqService.add(ywYwfySq);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("audit1")
    public ApiResult audit1(@RequestBody YwYwfySq ywYwfySq) {
        ywYwfySqService.audit1(ywYwfySq);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("audit2")
    public ApiResult audit2(@RequestBody YwYwfySq ywYwfySq) {
        ywYwfySqService.audit2(ywYwfySq);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("audit3")
    public ApiResult audit3(@RequestBody YwYwfySq ywYwfySq) {
        ywYwfySqService.audit3(ywYwfySq);
        return ApiResult.ok();
    }
    @ResponseBody
    @PostMapping("page")
    public ApiResult page(@RequestBody YwYwfySqVo ywYwfySqVo) {
        IPage<YwYwfySq> page = ywYwfySqService.page(ywYwfySqVo);
        return ApiResult.ok().data(page);
    }
    @ResponseBody
    @PostMapping("fytj")
    public ApiResult fytj(@RequestBody YwYwfySqVo vo) {
        List<YwYwfySqDto> fytj = ywYwfySqService.fytj(vo);
        BigDecimal fytjzj = ywYwfySqService.fytjzj(vo);

        Map<String,Object> map=new HashMap();
        map.put("sj",fytj);
        map.put("zje",fytjzj);
        return ApiResult.ok().data(map);
    }

    @ResponseBody
    @PostMapping("fytj/daochu")
    public ApiResult fytjDaochu(@RequestBody YwYwfySqVo vo) {
        List<YwYwfySqDto> fytj = ywYwfySqService.fytj(vo);

        ExcelUtil.doWrite("费用统计","费用统计",YwYwfySqDto.class,fytj);
        return ApiResult.ok();
    }

    @ResponseBody
    @PostMapping("/get/daochu")
    public ApiResult getDaochu(@RequestBody YwGdxxVo vo, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        YwYwfySq ywYwfySq = ywYwfySqService.get(vo.getId());
//        if (ywYwfySq.getQxAuditState()!=null){
//            throw new MyException("只能导出审核完成的申请");
//        }
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("报警详情", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        ywYwfySqService.getDaochu(ywYwfySq, response);
        return ApiResult.ok();
    }
}

